import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {NumberValidator} from "../../app/common/number.validatior";
import {ToastService} from "../../app/common/toast.service";

import {MobileHttp} from "./mobilehttp.serviice";

@Component({
  selector: 'page-mobile',
  templateUrl: 'mobile.html',
  providers:[MobileHttp]

})
export class MobilePage {

  private numValidator: NumberValidator;
  public attrResult:Array<string>;
  constructor(public navCtrl: NavController, private toast: ToastService,
              private mobileHttp: MobileHttp) {
    this.numValidator = new NumberValidator();
  }

  public onMobileSubmit(f: any) {
    let mobileNum = f["mobileInput"];
    let flag = this.numValidator.forVerify(mobileNum);
    if (!flag) {
      this.toast.makeToast("不能包含非数字字符", ToastService.MIDDLE_DURATION);
      return;
    }
    this.attrResult = [];
    this.mobileHttp.searchAttribution(mobileNum).subscribe(
      (res) =>{
        console.log(res)
        let result = res['result'];
        this.attrResult = res.toArray;
        let  attrs : Array<string> = new Array<string>();
        attrs[0] = "号码：" + result['shouji']
        attrs[1] = "归属地："+result['province']+result['city'];
        attrs[2] = '运营商：'+result['company'];
        attrs[3] = '区号：'+ result['areacode'];
        attrs[4] = "类型："+result['cardtype'];
        this.attrResult = attrs;
      },
      ()=>{
        console.log("请求完成")
      }
    );
  }
}
